package com.ruoyi.web.controller.school;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.school.Exception.BussnissExecption;
import com.ruoyi.school.domain.*;
import com.ruoyi.school.service.ILoveService;
import com.ruoyi.school.service.ISchoolNewsService;
import com.ruoyi.school.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/font")
public class SchoolFontController extends BaseController {

    @Autowired
    ISchoolNewsService schoolNewsService;

    @Autowired
    private ILoveService loveService;

    @Autowired
    private IUserService userService;

    @PostMapping("/login")
    public AjaxResult loginCheck(@RequestBody WeChatModel weChatModel, HttpServletResponse response, HttpServletRequest request) {
        return success(userService.checkLogin(weChatModel.getCode(),request));
    }

    @GetMapping("/list")
    public TableDataInfo list(Love love)
    {
        startPage();
        List<Love> list = loveService.selectLoveList(love);
        return getDataTable(list);
    }

    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id,@RequestParam(value = "loginId",required = false) Long loginId)
    {
        return success(loveService.selectLoveById(id,loginId));
    }

    @GetMapping(value = "/getCommon/{id}")
    public TableDataInfo getCommon(@PathVariable("id") Long id)
    {
        return getDataTable(loveService.getCommon(id));
    }

    @GetMapping(value = "/zan/{loveId}")
    public AjaxResult zan(@PathVariable("loveId") Long loveId,  User user)
    {
        return success(loveService.zan(loveId,user));
    }

    @GetMapping(value = "/guan")
    public AjaxResult guan(Guanzhu guanzhu)
    {
        return success(loveService.guan(guanzhu));
    }

    @GetMapping(value = "/messageList")
    public AjaxResult messageList(Long userId)
    {
        if (StringUtils.isEmpty(String.valueOf(userId))){
            throw new BussnissExecption("请先登录");
        }
        return success(schoolNewsService.selectSchoolNewsListById(userId));
    }

    @GetMapping(value = "/read")
    public AjaxResult messageRead(Long id)
    {
        SchoolNews schoolNews = new SchoolNews();
        schoolNews.setId(id);
        schoolNews.setStatus("1");
        return success(schoolNewsService.updateSchoolNews(schoolNews));
    }


    @PostMapping("/auth/save")
    public AjaxResult handleFileUpload(MultipartHttpServletRequest request) {
        MultipartFile file = request.getFile("file");
        if (file.isEmpty()) {
            System.err.println(1);
        }
        // 保存文件到服务器，这里省略了保存文件的代码
        Map<String, String[]> formData = request.getParameterMap();
        // 获取额外的表单数据
        String nickName = formData.getOrDefault("nickname", new String[0])[0];
        String code = formData.getOrDefault("code", new String[0])[0];
        return success(schoolNewsService.handleFileUpload(file,nickName,code));
    }

    @GetMapping(value = "/getGuanOrFen")
    public TableDataInfo getGuanOrFen(Long index,Long userId)
    {
        if (StringUtils.isEmpty(String.valueOf(userId))){
            throw new BussnissExecption("请先登录");
        }
        startPage();
        List<User> guanOrFen = userService.getGuanOrFen(index, userId);
        return getDataTable(guanOrFen);
    }

    @GetMapping(value = "/getLikeOrPush")
    public TableDataInfo getLikeOrPush(Long index,Long userId,Long pushAble,String content)
    {
        if (StringUtils.isEmpty(String.valueOf(userId))){
            throw new BussnissExecption("请先登录");
        }
        startPage();
        List<Love> getLikeOrPush = loveService.getLikeOrPush(index, userId,pushAble,content);
        return getDataTable(getLikeOrPush);
    }


    @PostMapping(value = "/commontAdd")
    public AjaxResult commontAdd(@RequestBody Common common)
    {
        return success(loveService.addCommon(common));
    }

    @GetMapping(value = "/getTwoHeadType")
    public TableDataInfo getTwoHeadType()
    {
        return getDataTable(loveService.gettype());
    }

    @GetMapping(value = "/getLoveByPersion")
    public TableDataInfo getLoveByPersion(Long userId,Long myId)
    {
        startPage();
        return getDataTable(loveService.getLoveByPersion(userId,myId));
    }

    @PostMapping(value = "/loveAdd")
    public AjaxResult loveAdd(@RequestBody Love love)
    {
        return success(loveService.loveAdd(love));
    }


    @GetMapping(value = "/removeDian")
    public AjaxResult removeDian(Long userId,Long dianId)
    {
        return success(loveService.removeDian( userId, dianId));
    }


    @GetMapping(value = "/removeLovePush")
    public AjaxResult removeLovePush(Long loveId)
    {
        return success(loveService.removeLovePush(loveId));
    }

    @GetMapping(value = "/pressImage")
    public void pressImage() throws IOException {
         loveService.pressImag();
    }

}
